<?php

class View_Simple implements View_Interface {
	protected $_path = '';
	protected $_data = array();
	
	public function __construct() {}
	
	public function setPath($path) {
		$this->_path = $path;
	}
	
	public function getPath() {
		return $this->_path;
	}
	
	public function assign($name, $value = null) {
		if (is_array($name)) {
			$this->_data = array_merge($this->_data, $name);
		} elseif (is_string($name)) {
			$this->_data[$name] = $value;
		} else {
			throw new View_Exception("argument 1 passed to View_Simple::assign() must be an array or string, ".gettype($name)." given.");
		}
	}
	
	/**
	 * 渲染模版并返回结果
	 * @param string $tempfile 模版文件名
	 */
	public function fetch($tpl) {
		$tpl = $this->_path . $tpl;
		if (!is_file($tpl)) {
			throw new View_Exception("view file {$tpl} not exists.");
		}
		ob_start();
		ob_implicit_flush(0);
		extract($this->_data, EXTR_OVERWRITE);
		include $tpl;
		$content = ob_get_clean();
		return $content;
	}
	
	/**
	 * 显示页面视图
	 * @param string $tempfile 模版文件名称
	 * @param string $charset 字符集
	 * @param string $contentType 输出文件类型
	 */
	public function display($tpl) {
		if (!headers_sent()) {
			header('Cache-control: private');
			header('X-Powered-By:JBlog');
		}
        echo $this->fetch($tpl);
	}
}